package com.seaboxdata.interfaceJob;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.seaboxdata.entity.AirSite;
import com.seaboxdata.service.AirSiteService;
import com.seaboxdata.utils.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @author
 * @create 2021-04-22 15:17
 **/
@Component
@Configuration
@Slf4j
public class AirSiteJob {

    @Value("${sthj.airSite.url}")
    private String url;

    @Value("${sthj.airSite.pageSize}")
    private String pageSize;

    @Autowired
    private AirSiteService airSiteService;

    @Value("${interface.isTest}")
    private String isTest;

    @Scheduled(cron = "${sthj.airSite.interval}")
    public void collect() {
        log.info("get airSite begin");
        if("true".equals(isTest)){
            return;
        }
        Integer maxRoeId = airSiteService.getMaxRowId();
        if (maxRoeId == null) {
            maxRoeId = 0;
        }
        handler(pageSize, url, "1", maxRoeId);
    }

    public void handler(String pageSize, String url, String pageNum, Integer maxRowId) {
        Map<String, Object> map = new HashMap();
        map.put("pageSize", pageSize);
        map.put("pageNum", pageNum);

        try {
            String resultStr = HttpUtil.doPost(url, map);
            if (resultStr != null) {
                JSONObject jsonObject = JSON.parseObject(resultStr);
                if ("200".equals((String) ((JSONObject) jsonObject.get("head")).get("statusCode"))) {
                    boolean hasNextPage = (Boolean) ((JSONObject) ((JSONObject) jsonObject.get("head")).get("pageInfo")).get("hasNextPage");
                    JSONArray data = (JSONArray) jsonObject.get("data");
                    List<AirSite> airSites = new ArrayList<>();
                    Iterator<Object> iterator = data.iterator();
                    while (iterator.hasNext()) {
                        JSONObject result = (JSONObject) iterator.next();
                        AirSite airSite = result.toJavaObject(AirSite.class);
                        if (airSite.getRowId() > maxRowId) {
                            airSites.add(airSite);
                        }
                    }
                    if (!CollectionUtils.isEmpty(airSites)) {
                        airSiteService.saveBatch(airSites);
                    }
                    if (hasNextPage) {
                        handler(pageSize, url, Integer.valueOf(pageNum) + 1 + "", maxRowId);
                    }
                } else {
                    log.error("get ariSite error:" + ((JSONObject) jsonObject.get("head")).get("message"));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
